from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext  import db

from ironcap.data.nodes      import ReferenceNode
from ironcap.data.counters   import Counter
from ironcap.data.tags       import TagList
from ironcap.data.tags       import Tag
from ironcap.data.votes      import Votes

from ironcap.interop import json

class Page(ReferenceNode):
	title   = db.StringProperty()
	counter = db.ReferenceProperty(Counter)
	tags    = db.ReferenceProperty(TagList)
	votes   = db.ReferenceProperty(Votes)
	
	@classmethod
	def create(cls, **kwds):
		""" deletes this page and associated references """
		page         = super(Page, cls).create(**kwds)
		page.counter = Counter.create(reference=page)
		page.tags    = TagList.create(reference=page)
		page.votes   = Votes.create(reference=page)
		page.put()
		return page

	def delete(self):
		try:
			if self.tags:
				self.tags.delete()
		except:
			pass
		try:
			if self.counter:
				self.counter.delete()
		except:
			pass
		try:
			if self.votes:
				self.votes.delete()
		except:
			pass
		ReferenceNode.delete(self)			

class MyClass(object):
	var0 = "asd"
	var1 = 0
	@staticmethod
	def method0(a, b):
		return a + b
	
	@staticmethod
	def method1(a):
		pass
	

class Handler(webapp.RequestHandler):
	
	
	def reflect(self, cls):
		print cls.__name__
		for n in cls.__dict__.iteritems():
			attr = getattr(cls, n[0])
			if callable(attr):
				print n[0]
				print attr.func_code.co_varnames		
	
	def get(self):
		
		obj = MyClass()
		self.reflect(MyClass)

		#self.response.out.write(json.to_json(page));
		


def main():
    application = webapp.WSGIApplication([('/', Handler)],
                                         debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()
